/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.kcrx.autoweightapp.models

import cn.kcrx.autoweightapp.utils.XToastUtils
import com.tencent.mmkv.MMKV
import mu.KotlinLogging

data class NewCashBox(val cashName: CashName,var weight: Float,var weightrange: Float) {

}

/***
 * 原封 券别列表
 */
enum class CashName{
   Cash100, Cash50, Cash20, Cash10, Cash5, Cash1
}
/***
 * 原封箱重量设定
 */
object NewCashBoxSetting{
    val settinglist= mutableListOf<NewCashBox>()
    private val logger=KotlinLogging.logger {  }
   init {
       settinglist.add(NewCashBox(CashName.Cash100,27.5f*20,0.6f*20))
       settinglist.add(NewCashBox(CashName.Cash50,23.2f*20,0.65f*20))
       settinglist.add(NewCashBox(CashName.Cash20,22.84f*20,0.2f*20))
       settinglist.add(NewCashBox(CashName.Cash10,21.81f*20,0.5f*20))
       settinglist.add(NewCashBox(CashName.Cash5,19.14f*20,0.4f*20))
       settinglist.add(NewCashBox(CashName.Cash1,18.46f*20,0.4f*20))
       loadSetting()
   }
   fun setWeight(i:Int,weight:Float,weightrange:Float){
       settinglist[i].weight=weight
       settinglist[i].weightrange=weightrange
   }
    fun getCashNameTxt(i:Int):String{
        return when(i){
            0-> "100元"
            1-> " 50元"
            2-> " 20元"
            3-> " 10元"
            4-> "  5元"
            5-> "  1元"
            else->throw IndexOutOfBoundsException("无此券别")
        }
    }
    fun saveSetting(){
        settinglist.forEach {
            if (it.weight <= it.weightrange) {
                XToastUtils.error("标定重量不能小于标定重量范围")
            } else {
                MMKV.defaultMMKV().encode("${it.cashName.ordinal}_weight", it.weight)
                MMKV.defaultMMKV().encode("${it.cashName.ordinal}_weight_range", it.weightrange)
                logger.error { "保存设置到${it.cashName} weight=${it.weight} weightrange=${it.weightrange}" }
            }
        }
    }
    fun loadSetting(){
        settinglist.forEach{
            if(MMKV.defaultMMKV().containsKey("${it.cashName.ordinal}_weight")){
                val weight=MMKV.defaultMMKV().decodeFloat("${it.cashName.ordinal}_weight",0.0f)
                val weightrange=MMKV.defaultMMKV().decodeFloat("${it.cashName.ordinal}_weight_range",0.0f)
                it.weight=weight
                it.weightrange=weightrange
                logger.error { "loadSetting-加载设置${it.cashName} weight=$weight weightrange=$weightrange" }
            }else{
                logger.error { "loadSetting-首次运行，使用默认配置,不加载" }
            }


        }
    }
}